package org.christopher.mybatisplusdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.christopher.mybatisplusdemo.common.UserCondition;
import org.christopher.mybatisplusdemo.entity.User;
import org.christopher.mybatisplusdemo.mapper.UserMapper;
import org.christopher.mybatisplusdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> selectUserAll() {
        return userMapper.selectList(null);
    }

    @Override
    public List<User> selectUserByCondition(UserCondition condition) {

        //多条件查询，可以在QueryWrapper后面添加条件。like就是模糊查询的意思
        QueryWrapper<User> queryWrapper = new QueryWrapper<>(new User())
                .like("name",condition.getName())
                .ge("cash",condition.getCash());

        return userMapper.selectList(queryWrapper);
    }

    @Override
    public List<User> selectUserByIds(List<Integer> ids) {

        ids.add(1);
        ids.add(2);
        List<User> users = userMapper.selectBatchIds(ids);

        return users;
    }

    @Override
    public User selectUserById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public User loginUser(User user) {
        //将我们传入的对象封装为Wrappe接口的实现类QueryWrapper的对象
        QueryWrapper<User> userWrapper = new QueryWrapper<>(user);
        return userMapper.selectOne(userWrapper);
    }

    @Override
    public int selectCount(User user) {
        QueryWrapper<User> userWrapper = new QueryWrapper<>(user);

        return userMapper.selectCount(userWrapper);
    }

    @Override
    public int addUser(User user) {
        return userMapper.insert(user);
    }

    @Override
    public int unpdateUser(User user) {

        return userMapper.updateById(user);
    }

    @Override
    public int unpdateUser2(User user) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        //设置要更新的字段和值，key是db的属性名称
        updateWrapper.set("cash",user.getCash());
        //条件
        updateWrapper.eq("name",user.getName());

        return userMapper.update(null,updateWrapper);
    }

    @Override
    public int delUser(User user) {
        return 0;
    }
}
